package com.xiaxueqi.web.home;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.context.SecurityContext;
import com.niagara.dao.Page;
import com.niagara.security.auth.entity.UserDetail;
import com.niagara.security.auth.exception.AuthException;
import com.niagara.service.base.BaseService;
import com.niagara.web.struts2.CRUDActionSupport;
import com.niagara.web.struts2.Struts2Utils;
import com.octo.captcha.service.CaptchaService;
import com.octo.captcha.service.CaptchaServiceException;
import com.xiaxueqi.entity.book.AppOrder;

@Results( { @Result(name = CRUDActionSupport.RELOAD, location = "book-order.action", type = "redirect") })
public class BookOrderAction extends CRUDActionSupport<AppOrder> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private BaseService baseService;
	private AppOrder entity;
	private Integer id;
	private Page<AppOrder> page;

	private UserDetail detail = SecurityContext.getUserDetail();
	private Integer userId = detail != null ? Integer.valueOf(detail
			.getUserId().intValue()) : null;

	private String captchaKey = "checkCode";
	private CaptchaService captchaService;
	private String captchaError;

	public String getCaptchaError() {
		return captchaError;
	}

	public CaptchaService getCaptchaService() {
		return captchaService;
	}

	public void setCaptchaService(CaptchaService captchaService) {
		this.captchaService = captchaService;
	}

	public Page<AppOrder> getPage() {
		return page;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	// 用户求购列表的逻辑删除
	@Override
	public String delete() throws Exception {
		this.entity = this.baseService.get(AppOrder.class, this.id);
		this.entity.setDeleted("Y");
		this.baseService.update(this.entity);
		return RELOAD;
	}

	// 用户求购列表，可以管理自己图书求购信息
	@SuppressWarnings("unchecked")
	@Override
	public String list() throws Exception {
		Map map = Struts2Utils.getParameters();
		Integer pageNo = Struts2Utils.getIntParameter(PARAMS_PAGENO, Integer
				.valueOf(1));
		map.put("userId", userId);
		this.page = this.baseService.findForPaging(AppOrder.class, map,
				pageNo, DEFAULT_PAGE_SIZE);
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (this.id != null) {
			this.entity = this.baseService.get(AppOrder.class, this.id);
		} else {
			this.entity = new AppOrder();
		}
	}

	// 可以用来发布自己的求购信息

	@Override
	public String save() throws Exception {
		HttpServletRequest request = SecurityContext.getRequest();
		String captcha = request.getParameter(this.captchaKey);
		String captchaID = request.getSession().getId();
		boolean success = false;
		try {
			success = captchaService.validateResponseForID(captchaID, captcha);
		} catch (CaptchaServiceException e) {
			logger.error(e.getMessage(), e);
			throw new AuthException(e);
		}
		if (!success) {
			this.captchaError = "error";
			// return 一个没有定义的跳转"stop"，将只将本页面刷新
			return "stop";
		}
		if (entity.getId() == null) {
			this.entity.setUserId(userId);
			baseService.insert(entity);
		} else {
			this.entity.setUserId(userId);
			baseService.update(entity);
		}
		addActionMessage("保存成功");
		return RELOAD;
	}

	public AppOrder getModel() {
		return this.entity;
	}
}
